from configs import errorCode, config
from models import goods_collect
from views import BaseHandler
from utils.util import json_decode


class GoodsCollect(BaseHandler):

    @BaseHandler.auth
    async def get(self):
        """
        ---
        tags:
          - 商品收藏
        summary: 查看商品收藏
        description:
        parameters:
        - in: cookie
          name: user_token
          schema:
            type: string
            default: null
        responses:
          '200':
            description: successful operation
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/OkModel'
          '400':
            description: error operation
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/ErrorModel'
        """

        # 查询用户商品收藏
        spu_ids = await config.redis.lrange('collect:%s' % self.current_user, 0, -1)
        spu_ids = list(spu_ids)
        spus = await goods_collect.get_goods_collect(spu_ids)
        return self.write_json(data=spus)

    @BaseHandler.auth
    async def post(self):
        """
        ---
        tags:
          - 商品收藏
        summary: 添加收藏
        description:
        requestBody:
          description:
          content:
            application/json:
                schema:
                  type: object
                  properties:
                    spu_id:
                      type: integer
        responses:
          '200':
            description: successful operation
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/OkModel'
          '400':
            description: error operation
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/ErrorModel'
        """
        # 接收参数
        json_dict = json_decode(self.request.body)
        spu_id = json_dict.get('spu_id')
        # 判断spu_id商品是否存在
        res = await goods_collect.query_goods_is_exist(spu_id)
        if not res:
            return self.write_error(errorCode.NOT_GOODS, '商品不存在')
        # 保存spu_id到redis
        # 先去重
        pl = config.redis.pipeline()
        pl.lrem('collect:%s' % self.current_user, 0, spu_id)
        # 再保存， 最近收藏的商品在最前面
        pl.lpush('collect:%s' % self.current_user, spu_id)
        await pl.execute()
        # 响应结果
        return self.write_json()

    @BaseHandler.auth
    async def delete(self):
        """
        ---
        tags:
          - 商品收藏
        summary: 删除商品收藏
        description:
        requestBody:
          description:
          content:
            application/json:
                schema:
                  type: object
                  properties:
                    spu_id:
                      type: list
        responses:
          '200':
            description: successful operation
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/OkModel'
          '400':
            description: error operation
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/ErrorModel'
        """
        # 接收参数
        json_dict = json_decode(self.request.body)
        spu_ids = json_dict.get('spu_ids')
        spu_ids = list(spu_ids)
        # 判断spu_id商品是否存在
        res = await goods_collect.query_goods_is_exist(spu_ids)
        if not res:
            return self.write_error(errorCode.NOT_GOODS, '商品不存在')
        # 删除redis中对就的spu_id
        pl = config.redis.pipeline()
        for spu_id in spu_ids:
            pl.lrem('collect:%s' % self.current_user, 0, spu_id)
        await pl.execute()
        return self.write_json()


class DeleteCollect(BaseHandler):

    @BaseHandler.auth
    async def post(self):
        """
        ---
        tags:
          - 商品收藏
        summary: 删除商品收藏
        description:
        requestBody:
          description:
          content:
            application/json:
                schema:
                  type: object
                  properties:
                    spu_id:
                      type: list
        responses:
          '200':
            description: successful operation
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/OkModel'
          '400':
            description: error operation
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/ErrorModel'
        """
        # 接收参数
        json_dict = json_decode(self.request.body)
        spu_ids = json_dict.get('spu_id')
        spu_ids = list(spu_ids)
        # 判断spu_id商品是否存在
        res = await goods_collect.query_goods_is_exist(spu_ids)
        if not res:
            return self.write_error(errorCode.NOT_GOODS, '商品不存在')
        # 删除redis中对就的spu_id
        pl = config.redis.pipeline()
        for spu_id in spu_ids:
            pl.lrem('collect:%s' % self.current_user, 0, spu_id)
        await pl.execute()
        return self.write_json()
